package cz.boxdesing.vizitky.server.common.impl;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
import org.springframework.ejb.support.AbstractStatefulSessionBean;

public class BoxStatefullBean extends AbstractStatefulSessionBean implements SessionBean {

  private static final long serialVersionUID = -7588614410343638809L;
  private String username;

  @Override
  public void setSessionContext(SessionContext sessionContext) {
    super.setSessionContext(sessionContext);
    setBeanFactoryLocator(ContextSingletonBeanFactoryLocator.getInstance());
    setBeanFactoryLocatorKey(BoxSessionBean.PRIMARY_CONTEXT_ID);
  }

  private void load() {
    loadBeanFactory();
  }

  private void unload() {
    unloadBeanFactory();
    setBeanFactoryLocator(null);
  }

  public void ejbCreate() throws CreateException {
    load();
  }

  public void ejbCreate(java.lang.String parUsername) throws CreateException {
    ejbCreate();
    username = parUsername;
  }

  public void ejbRemove() throws EJBException {
  }

  public void ejbActivate() throws EJBException {
    load();
  }

  public void ejbPassivate() throws EJBException {
    unload();
  }

  public boolean hasMessages() {
    return false;
  }

}
